import wx
import abc
import time
from DataManager import DataManager
from SimpleDialogs import DialogAddItem
#from SimpleMenus import ListPopupMenu
from ModelBaseList import ListItem


class ControlBaseList():
    """
    the idea is to put as much code into base class as possible,
    including menu creation, so there won't be any code redundance in
    subclasses
    """

    __metaclass__ = abc.ABCMeta



    @classmethod
    def get_model(cls):
        """
        subclass MUST define the get_model method I can't use 2 decorators to
        implement this, so I can't use abstractmethod.
        """
        print("this is fatally wrong! subclass MUST override this")

    @classmethod
    def control_edit_enter(cls, self, evt, style):
        """
        handle event when enter is pressed in txtctrl
        """
        ret = []

        #get the rest information(we already have title) from dialog
        org = ListItem.get_template()
        org[0] = self.ui_edit.GetValue()
        d = DialogAddItem(ret, style, org)
        r = d.ShowModal()
        d.Destroy()
        if r != 0:
            #cancel case: do nothing here
            pass
        else:
            i = cls.get_model().build_append(ret)

    @classmethod
    def control_list_right_click(cls, self, evt, menu_class):
        """
        subclasses should override this
        """
        action = ["DO_NOTHING"]
        pos = evt.GetItem().GetData()
        #popup menu
        menu = menu_class(action)
        self.PopupMenu(menu, evt.GetPoint())
        menu.Destroy()
        return action, pos


    @classmethod
    def control_list_double_click(cls, self, evt, style = DialogAddItem.WISHLIST_MODE):
        #print("double click row:")
        #print(evt.GetItem().GetData())
        d = self.data_list[evt.GetItem().GetData()]
        ret = []
        #status, ctime and mtime are not needed
        dialog = DialogAddItem(ret, style, d.to_list()[0:9])

        r = dialog.ShowModal()
        dialog.Destroy()
        return ret, d, r

    @classmethod
    def control_list_column_click(cls, self, evt):
        #print(evt.GetColumn())
        col = evt.GetColumn()
        if col < 0 or col >= len(self.fields):
            #get lost
            return

        self.data_list.sort(self.col_cmper[col], reverse=self.order_of_colomn[col])
        #toggle this
        self.order_of_colomn[col] = not self.order_of_colomn[col]
        self.update()

    @classmethod
    def control_move_to_other_list(cls, item, dst):
        #original will not be deleted by us
        #we just do the if else add work
        if dst == 'TODO':
            DataManager.todolist.direct_append(item)
        elif dst == 'COLLECT':
            DataManager.collectlist.direct_append(item)
        elif dst == 'WISH':
            DataManager.wishlist.direct_append(item)
        else:
            print("unknown move to destination: %s"%(dst))
        
